<?php
// Copyright 2011 Zeptonic Ek. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
//    conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
//    of conditions and the following disclaimer in the documentation and/or other materials
//    provided with the distribution.
// 
// THIS SOFTWARE IS PROVIDED BY Zeptonic Ek ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Zeptonic Ek OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 
// The views and conclusions contained in the software and documentation are those of the 
// authors and should not be interpreted as representing official policies, 
// either expressed or implied, of Zeptonic Ek.

/**
 * Css nod.
 * Representerar en nod i css-filen.
 * 
 * @category Golfscraper
 * @package  Golfscraper
 * @author   Anders Axelsson <anders@zeptonic.com>
 * @author   Mattias Tiger <mattias@zeptonic.com>
 * @link     http://www.zeptonic.com
 */
class Css_node {
	/**
	 * @var String Typ av css-nod (class, id, tag)
	 */
	private $type;
	
	/**
	 * @var String Namn på css-nod
	 */
	private $name;
	
	/**
	 * @var Array Lista med alla nodens attribut
	 */
	private $attributes;
	
	/**
	 * Skapar en css nod genom att sätta typ och namn av nod samt 
	 * initiera en lista med attribut.
	 *
	 * @param String $type Typ av nod (class, id, tag)
	 * @param String $name Namn på nod
	 *
	 * @return Void
	 */
	public function __construct($type, $name) {
		$this->type = $type;
		$this->name = $name;
		$this->attributes = array();
	}
	
	/**
	 * Hämtar nodens namn.
	 *
	 * @Return String
	 */
	public function get_name() {
		return $this->name;
	}
	
	/**
	 * Hämtar ett attributs värde.
	 *
	 * @param String $name Namn på attributet
	 *
	 * @Return String
	 */
	public function get_attribute($name) {
		return $this->attributes[$name];
	}
	
	/**
	 * Kontrollerar om ett visst attribut finns eller inte.
	 * 
	 * @param String $name Namn på attributet
	 *
	 * @Return Boolean
	 */
	public function has_attribute($name) {
		return isset($this->attributes[$name]);
	}
	
	/**
	 * Skapar ett attribut.
	 * 
	 * @param String $name Namn på attributet
	 * @param String $value Värde till attributet
	 *
	 * @Return Void
	 */
	public function set_attribute($name, $value) {
		$this->attributes[$name] = $value;
	}
	
	/**
	 * Tar bort ett attribut.
	 * 
	 * @param String $name Namn på attributet
	 *
	 * @Return Void
	 */
	public function unset_attribute($name) {
		unset($this->attributes[$name]);
	}
	
	/**
	 * Hämta alla attribut.
	 * 
	 * @Return Array
	 */
	public function get_attributes() {
		return $this->attributes;
	}
	
	/**
	 * Konverterar data i en css-nod till attribut och lagrar dessa i noden.
	 * 
	 * @param String $data Data med attribut att konvertera till attribut
	 *
	 * @Return Void
	 */
	public function load($data) {
		$data = trim($data);
		$attrs = explode(";", $data);
		
		// tar bort sista attributet om det är tomt
		if(empty($attrs[count($attrs) - 1])) {
			unset($attrs[count($attrs) - 1]);
		}
		
		foreach($attrs as $key => $attr) {
			$first_colon_pos = strpos($attr, ":");
			$attr_name = trim(substr($attr, 0, $first_colon_pos));
			$attr_value = trim(substr($attr, $first_colon_pos + 1, strlen($attr)));
			$this->attributes[$attr_name] = $attr_value;
		}
	}
}
?>